package com.hdoit.kylin.logical.user.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;



import com.tennetcn.common.dao.base.impl.SuperService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.message.SearchItem;
import com.tennetcn.common.message.SearchModel;
import com.hdoit.kylin.logical.user.dao.IContactDao;
import com.hdoit.kylin.logical.user.model.Contact;
import com.hdoit.kylin.logical.user.model.Linker;
import com.hdoit.kylin.logical.user.service.IContactService;
import com.hdoit.kylin.logical.user.service.IStudentService;
import com.hdoit.kylin.logical.user.service.ITeacherService;

/**
 * @author admin
 * @email
 * @createtime 2017年07月25日 15:47:22
 * @comment
 */
@Component
public class ContactServiceImpl extends SuperService<Contact> implements IContactService {
	private IContactDao contactDao;
	private ITeacherService teacherService;
	private IStudentService studentService;
	
	public IContactDao getContactDao() {
		return contactDao;
	}

	@Resource
	public void setContactDao(IContactDao contactDao) {
		this.contactDao = contactDao;
	}

	@Override
	public List<Contact> queryListByGroupId(SearchModel search, String groupId) {
		return contactDao.queryListByGroupId(search, groupId);
	}

	public ITeacherService getTeacherService() {
		return teacherService;
	}

	@Resource
	public void setTeacherService(ITeacherService teacherService) {
		this.teacherService = teacherService;
	}

	public IStudentService getStudentService() {
		return studentService;
	}

	@Resource
	public void setStudentService(IStudentService studentService) {
		this.studentService = studentService;
	}

	@Override
	public List<Linker> queryLinkersByGroupIdNoContact(String groupId){
		return contactDao.queryLinkersByGroupIdNoContact(groupId);
	}
	
	@Override
	public List<Linker> queryLinkersByGroupId(SearchModel search,
			PagerModel pagerModel, String groupId) {
		return contactDao.queryLinkersByGroupId(search, pagerModel, groupId);
	}

	@Override
	public int queryCountByGroupId(SearchModel search, String groupId) {
		if(StringUtils.isEmpty(groupId))
			return queryCount(search);
		return contactDao.queryCountByGroupId(search, groupId);
	}

	@Override
	public int queryLinkersCountByGroupId(SearchModel search, String groupId) {
		int contactNum=queryCountByGroupId(search, groupId);
		int teacherNum=0,studentNum=0;
		boolean finish=false;
		if(null!=search){
			List<SearchItem> searchItems=search.getSearchItemList();
			if(null!=searchItems&&!StringUtils.isEmpty(search.getSearchKey())){
				for(SearchItem searchItem:searchItems){
					if(searchItem.isToSearch()){
						if("contactName".equals(searchItem.getColumnName())){
							searchItem.setColumnName("teacherName");
							teacherNum=teacherService.queryCountByGroupId(search, groupId);
							searchItem.setColumnName("studentName");
							studentNum=studentService.queryCountByGroupId(search, groupId);
							searchItem.setColumnName("contactName");
							finish=true;
							break;
						}
					}
				}
			}
		}
		if(!finish){
			teacherNum=teacherService.queryCountByGroupId(search, groupId);
			studentNum=studentService.queryCountByGroupId(search, groupId);
		}
		return contactNum+teacherNum+studentNum;
	}
}